{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c186a2f",
   "metadata": {},
   "source": [
    "# 导包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5771f27b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06e62944",
   "metadata": {},
   "source": [
    "# 读取数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "75f4223f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low    volume  price_change  p_change  \\\n",
      "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
      "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
      "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
      "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
      "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
      "\n",
      "               ma5    ma10    ma20     v_ma5    v_ma10    v_ma20  turnover  \n",
      "2018-02-27  22.942  22.142  22.875  53782.64  46738.65  55576.11      2.39  \n",
      "2018-02-26  22.406  21.955  22.942  40827.52  42736.34  56007.50      1.53  \n",
      "2018-02-23  21.938  21.929  23.022  35119.58  41871.97  56372.85      1.32  \n",
      "2018-02-22  21.446  21.909  23.137  35397.58  39904.78  60149.60      0.90  \n",
      "2018-02-14  21.366  21.923  23.253  33590.21  42935.74  61716.11      0.58  \n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('./stock_day.csv')\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "42de0d57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['2018-02-27', '2018-02-26', '2018-02-23', '2018-02-22', '2018-02-14',\n",
      "       '2018-02-13', '2018-02-12', '2018-02-09', '2018-02-08', '2018-02-07',\n",
      "       ...\n",
      "       '2015-03-13', '2015-03-12', '2015-03-11', '2015-03-10', '2015-03-09',\n",
      "       '2015-03-06', '2015-03-05', '2015-03-04', '2015-03-03', '2015-03-02'],\n",
      "      dtype='object', length=643)\n"
     ]
    }
   ],
   "source": [
    "print(df.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8210b05a",
   "metadata": {},
   "source": [
    "## 索引使用\n",
    "### df[列名][行名]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5d0eaca0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "2018-02-14    21.49\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(df['open'].head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc00c7ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23.53\n"
     ]
    }
   ],
   "source": [
    "print(df['open']['2018-02-27'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb41cdc",
   "metadata": {},
   "source": [
    "## loc\n",
    "### loc[行,列]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ab92abb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23.53\n"
     ]
    }
   ],
   "source": [
    "print(df.loc['2018-02-27', 'open'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1591c681",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 推测原代码想使用 .loc 方法进行标签索引，修正拼写错误并添加必要逗号分隔\n",
    "print(df.loc['2018-02-27':'2018-02-22', 'open'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b78caa5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low\n",
      "2018-02-27  23.53  25.88  24.16  23.53\n",
      "2018-02-26  22.80  23.78  23.53  22.80\n",
      "2018-02-23  22.88  23.37  22.82  22.71\n",
      "2018-02-22  22.25  22.76  22.28  22.02\n"
     ]
    }
   ],
   "source": [
    "print(df.loc['2018-02-27':'2018-02-22', 'open':'low'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bd15c755",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low    volume  price_change  p_change  \\\n",
      "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
      "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
      "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
      "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
      "\n",
      "               ma5    ma10    ma20     v_ma5    v_ma10    v_ma20  turnover  \n",
      "2018-02-27  22.942  22.142  22.875  53782.64  46738.65  55576.11      2.39  \n",
      "2018-02-26  22.406  21.955  22.942  40827.52  42736.34  56007.50      1.53  \n",
      "2018-02-23  21.938  21.929  23.022  35119.58  41871.97  56372.85      1.32  \n",
      "2018-02-22  21.446  21.909  23.137  35397.58  39904.78  60149.60      0.90  \n"
     ]
    }
   ],
   "source": [
    "print(df.loc['2018-02-27':'2018-02-22', :])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b6cdc19",
   "metadata": {},
   "source": [
    "## iloc\n",
    "iloc[行, 列的标号]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5cb806c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low\n",
      "2018-02-27  23.53  25.88  24.16  23.53\n",
      "2018-02-26  22.80  23.78  23.53  22.80\n",
      "2018-02-23  22.88  23.37  22.82  22.71\n",
      "2018-02-22  22.25  22.76  22.28  22.02\n"
     ]
    }
   ],
   "source": [
    "print(df.iloc[:4, 0:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97fa2281",
   "metadata": {},
   "source": [
    "# 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "689b820f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-27</th>\n",
       "      <td>23.53</td>\n",
       "      <td>25.88</td>\n",
       "      <td>24.16</td>\n",
       "      <td>23.53</td>\n",
       "      <td>95578.03</td>\n",
       "      <td>0.63</td>\n",
       "      <td>2.68</td>\n",
       "      <td>2.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-26</th>\n",
       "      <td>22.80</td>\n",
       "      <td>23.78</td>\n",
       "      <td>23.53</td>\n",
       "      <td>22.80</td>\n",
       "      <td>60985.11</td>\n",
       "      <td>0.69</td>\n",
       "      <td>3.02</td>\n",
       "      <td>1.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-23</th>\n",
       "      <td>22.88</td>\n",
       "      <td>23.37</td>\n",
       "      <td>22.82</td>\n",
       "      <td>22.71</td>\n",
       "      <td>52914.01</td>\n",
       "      <td>0.54</td>\n",
       "      <td>2.42</td>\n",
       "      <td>1.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-22</th>\n",
       "      <td>22.25</td>\n",
       "      <td>22.76</td>\n",
       "      <td>22.28</td>\n",
       "      <td>22.02</td>\n",
       "      <td>36105.01</td>\n",
       "      <td>0.36</td>\n",
       "      <td>1.64</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-14</th>\n",
       "      <td>21.49</td>\n",
       "      <td>21.99</td>\n",
       "      <td>21.92</td>\n",
       "      <td>21.48</td>\n",
       "      <td>23331.04</td>\n",
       "      <td>0.44</td>\n",
       "      <td>2.05</td>\n",
       "      <td>0.58</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low    volume  price_change  p_change  \\\n",
       "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
       "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
       "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
       "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
       "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
       "\n",
       "            turnover  \n",
       "2018-02-27      2.39  \n",
       "2018-02-26      1.53  \n",
       "2018-02-23      1.32  \n",
       "2018-02-22      0.90  \n",
       "2018-02-14      0.58  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(['v_ma20','v_ma10','v_ma5','ma20','ma10','ma5'], axis=1, inplace=True)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "021ad122",
   "metadata": {},
   "source": [
    "## 单列值排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "76e99571",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low    volume  price_change  p_change  \\\n",
      "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
      "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
      "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
      "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
      "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
      "\n",
      "            turnover  \n",
      "2018-02-27      2.39  \n",
      "2018-02-26      1.53  \n",
      "2018-02-23      1.32  \n",
      "2018-02-22      0.90  \n",
      "2018-02-14      0.58  \n"
     ]
    }
   ],
   "source": [
    "df.sort_values('open', ascending=True)\n",
    "print(df.head())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "842299c6",
   "metadata": {},
   "source": [
    "## 多列排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "464f06fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-27</th>\n",
       "      <td>23.53</td>\n",
       "      <td>25.88</td>\n",
       "      <td>24.16</td>\n",
       "      <td>23.53</td>\n",
       "      <td>95578.03</td>\n",
       "      <td>0.63</td>\n",
       "      <td>2.68</td>\n",
       "      <td>2.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-26</th>\n",
       "      <td>22.80</td>\n",
       "      <td>23.78</td>\n",
       "      <td>23.53</td>\n",
       "      <td>22.80</td>\n",
       "      <td>60985.11</td>\n",
       "      <td>0.69</td>\n",
       "      <td>3.02</td>\n",
       "      <td>1.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-23</th>\n",
       "      <td>22.88</td>\n",
       "      <td>23.37</td>\n",
       "      <td>22.82</td>\n",
       "      <td>22.71</td>\n",
       "      <td>52914.01</td>\n",
       "      <td>0.54</td>\n",
       "      <td>2.42</td>\n",
       "      <td>1.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-22</th>\n",
       "      <td>22.25</td>\n",
       "      <td>22.76</td>\n",
       "      <td>22.28</td>\n",
       "      <td>22.02</td>\n",
       "      <td>36105.01</td>\n",
       "      <td>0.36</td>\n",
       "      <td>1.64</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-14</th>\n",
       "      <td>21.49</td>\n",
       "      <td>21.99</td>\n",
       "      <td>21.92</td>\n",
       "      <td>21.48</td>\n",
       "      <td>23331.04</td>\n",
       "      <td>0.44</td>\n",
       "      <td>2.05</td>\n",
       "      <td>0.58</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low    volume  price_change  p_change  \\\n",
       "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
       "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
       "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
       "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
       "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
       "\n",
       "            turnover  \n",
       "2018-02-27      2.39  \n",
       "2018-02-26      1.53  \n",
       "2018-02-23      1.32  \n",
       "2018-02-22      0.90  \n",
       "2018-02-14      0.58  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(['open','high'], ascending=False)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cd3be988",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-27</th>\n",
       "      <td>23.53</td>\n",
       "      <td>25.88</td>\n",
       "      <td>24.16</td>\n",
       "      <td>23.53</td>\n",
       "      <td>95578.03</td>\n",
       "      <td>0.63</td>\n",
       "      <td>2.68</td>\n",
       "      <td>2.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-26</th>\n",
       "      <td>22.80</td>\n",
       "      <td>23.78</td>\n",
       "      <td>23.53</td>\n",
       "      <td>22.80</td>\n",
       "      <td>60985.11</td>\n",
       "      <td>0.69</td>\n",
       "      <td>3.02</td>\n",
       "      <td>1.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-23</th>\n",
       "      <td>22.88</td>\n",
       "      <td>23.37</td>\n",
       "      <td>22.82</td>\n",
       "      <td>22.71</td>\n",
       "      <td>52914.01</td>\n",
       "      <td>0.54</td>\n",
       "      <td>2.42</td>\n",
       "      <td>1.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-22</th>\n",
       "      <td>22.25</td>\n",
       "      <td>22.76</td>\n",
       "      <td>22.28</td>\n",
       "      <td>22.02</td>\n",
       "      <td>36105.01</td>\n",
       "      <td>0.36</td>\n",
       "      <td>1.64</td>\n",
       "      <td>0.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-14</th>\n",
       "      <td>21.49</td>\n",
       "      <td>21.99</td>\n",
       "      <td>21.92</td>\n",
       "      <td>21.48</td>\n",
       "      <td>23331.04</td>\n",
       "      <td>0.44</td>\n",
       "      <td>2.05</td>\n",
       "      <td>0.58</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low    volume  price_change  p_change  \\\n",
       "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
       "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
       "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
       "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
       "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
       "\n",
       "            turnover  \n",
       "2018-02-27      2.39  \n",
       "2018-02-26      1.53  \n",
       "2018-02-23      1.32  \n",
       "2018-02-22      0.90  \n",
       "2018-02-14      0.58  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(['open','high'], ascending=[True, False])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6a21ff8",
   "metadata": {},
   "source": [
    "## 行索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "835c7d7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low    volume  price_change  p_change  \\\n",
      "2018-02-27  23.53  25.88  24.16  23.53  95578.03          0.63      2.68   \n",
      "2018-02-26  22.80  23.78  23.53  22.80  60985.11          0.69      3.02   \n",
      "2018-02-23  22.88  23.37  22.82  22.71  52914.01          0.54      2.42   \n",
      "2018-02-22  22.25  22.76  22.28  22.02  36105.01          0.36      1.64   \n",
      "2018-02-14  21.49  21.99  21.92  21.48  23331.04          0.44      2.05   \n",
      "\n",
      "            turnover  \n",
      "2018-02-27      2.39  \n",
      "2018-02-26      1.53  \n",
      "2018-02-23      1.32  \n",
      "2018-02-22      0.90  \n",
      "2018-02-14      0.58  \n"
     ]
    }
   ],
   "source": [
    "df.sort_index()\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "64a8f236",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low     volume  price_change  p_change  \\\n",
      "2015-03-02  12.25  12.67  12.52  12.20   96291.73          0.32      2.62   \n",
      "2015-03-03  12.52  13.06  12.70  12.52  139071.61          0.18      1.44   \n",
      "2015-03-04  12.80  12.92  12.90  12.61   67075.44          0.20      1.57   \n",
      "2015-03-05  12.88  13.45  13.16  12.87   93180.39          0.26      2.02   \n",
      "2015-03-06  13.17  14.48  14.28  13.13  179831.72          1.12      8.51   \n",
      "\n",
      "            turnover  \n",
      "2015-03-02      3.30  \n",
      "2015-03-03      4.76  \n",
      "2015-03-04      2.30  \n",
      "2015-03-05      3.19  \n",
      "2015-03-06      6.16  \n"
     ]
    }
   ],
   "source": [
    "df.sort_index(inplace=True)\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "597816a2",
   "metadata": {},
   "source": [
    "## Series排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "031cd7cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "2018-02-14    21.49\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df['open'].sort_index()\n",
    "print(df['open'].head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7ac07582",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "2018-02-14    21.49\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df['open'].sort_index(inplace=True, ascending=False)\n",
    "print(df['open'].head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "248c8f44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "2018-02-14    21.49\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df.open.sort_index()\n",
    "print(df.open.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d960c862",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-27    23.53\n",
      "2018-02-26    22.80\n",
      "2018-02-23    22.88\n",
      "2018-02-22    22.25\n",
      "2018-02-14    21.49\n",
      "Name: open, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df.open.sort_values()\n",
    "print(df.open.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94a7a6a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
